//
//  ZYEmoticonPageView.swift
//  WBBeta1
//
//  Created by wustzhy on 16/8/18.
//  Copyright © 2016年 Yestin. All rights reserved.
//

import UIKit
//cell ID
private let ZYEmoticonPageViewCellId = "ZYEmoticonPageViewCellId"

class ZYEmoticonPageView: UICollectionView {

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        backgroundColor = RandomColor()
        dataSource = self
        registerClass(ZYEmoticonPageViewCell.self, forCellWithReuseIdentifier: ZYEmoticonPageViewCellId)
        
//        let flowLayout = self.collectionViewLayout as! UICollectionViewFlowLayout
//        flowLayout.itemSize = self.frame.size
//        negative or zero item sizes are not supported in the flow layout

    }
    override func layoutSubviews() {
        super.layoutSubviews()
        //设置flowLayout
        let flowLayout = self.collectionViewLayout as! UICollectionViewFlowLayout
        flowLayout.itemSize = self.frame.size
        flowLayout.minimumInteritemSpacing = 0
        flowLayout.minimumLineSpacing = 0
        flowLayout.scrollDirection = .Horizontal
    }
    

}


extension ZYEmoticonPageView : UICollectionViewDataSource {
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return ZYEmoticonTools.sharedTools.allEmoticons.count
    }
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return ZYEmoticonTools.sharedTools.allEmoticons[section].count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(ZYEmoticonPageViewCellId, forIndexPath: indexPath) as! ZYEmoticonPageViewCell
        cell.backgroundColor = RandomColor()
        cell.index = indexPath
        //赋值
        cell.emoticons = ZYEmoticonTools.sharedTools.allEmoticons[indexPath.section][indexPath.item]
        
        return cell
    }
    
    
}